<template>
  <d2-container>
    <div>
      <el-form :inline="true" class="demo-form-inline">
        <el-form-item>
          <el-select size="small" v-model="statusVal" placeholder="请选择审核状态">
            <el-option v-for="item in statuslist" :key="item.value" :label="item.label" :value="item.value"></el-option>
          </el-select>
        </el-form-item>
        <!--      <el-form-item>-->
        <!--        <el-select size="small" v-model="searchList.type" placeholder="请选择订单类型">-->
        <!--          <el-option v-for="item in statustype" :key="item.value" :label="item.label" :value="item.value"></el-option>-->
        <!--        </el-select>-->
        <!--      </el-form-item>-->
        <el-form-item>
          <el-input clearable placeholder="请输入订单号" size="small" v-model="searchList.searchValue"></el-input>
        </el-form-item>
        <el-form-item>
          <el-date-picker size="small" v-model="searchList.timelist" type="datetimerange" align="right" unlink-panels
                          range-separator="至"
                          value-format="yyyy-MM-dd HH-mm-ss"
                          start-placeholder="开始日期" end-placeholder="结束日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" size="mini" @click="initing">搜索</el-button>
        </el-form-item>
        <el-form-item style="margin-left:20px;">
          <el-radio @change="init(1)" fill v-model="radio1" label="1" border>淘宝</el-radio>
          <el-radio @change="init(2)" fill v-model="radio1" label="2" border>拼多多</el-radio>
          <el-radio @change="init(3)" fill v-model="radio1" label="3" border>抖音</el-radio>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="exportToExcel">导出订单</el-button>
        </el-form-item>
      </el-form>
      <el-table v-loading="loading" border
                @selection-change="handleSelectionChange" :data="tableData" style="width: 100%" header-align="center">
        <el-table-column type="selection" width="55"></el-table-column>
        <el-table-column type="index" label="序号" width="80" align="center"></el-table-column>
        <el-table-column prop="task_order" label="订单编号" align="center"></el-table-column>
        <el-table-column prop="task_class" label="订单类型" align="center">
          <template slot-scope="props">
            <p style="text-align: center;">{{props.row.task_class == 'pt' ?
              '普通单':(props.row.task_class=='ll'?'浏览单':(props.row.task_class == 'pdd'?'拼多多单':(props.row.task_class ==
              'dy'?'抖音单':'评价单')))}}</p>
          </template>
        </el-table-column>
        <el-table-column prop="task_shopname" label="店铺名" align="center"></el-table-column>
        <el-table-column prop="brushhands_tbname" label="刷手会员号" width="180" align="center"></el-table-column>
        <el-table-column prop="phone" label="刷手手机号" width="180" align="center"></el-table-column>
        <el-table-column prop="baby_id" label="宝贝ID" width="200" align="center"></el-table-column>
        <el-table-column prop="task_bjmoney" label="商品金额" width="120" align="center"></el-table-column>
        <el-table-column prop="create_time" label="创建时间" align="center"></el-table-column>
        <el-table-column fixed="right" label="审核" width="160" align="center">
          <template slot-scope="scope">
            <div v-if="scope.row.status == 1">
              <el-button @click="onPass(scope.row,1)" type="primary" plain size="mini">通过</el-button>
              <el-button @click="onPass(scope.row,2)" size="mini" type="danger" plain>拒绝</el-button>
            </div>
            <el-button v-if="scope.row.status == 2" type="success" size="mini" plain disabled>已通过</el-button>
            <el-button v-if="scope.row.status == 3" type="info" size="mini" plain disabled>已拒绝</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination background :page-size="100" @size-change="handleSizeChange" :page-sizes="[10, 50, 100, 300,500]" :current-page="page" layout="total, sizes, prev, pager, next, jumper"
                     :total="searchList.Count" @current-change="pnChange">
      </el-pagination>
    </div>
  </d2-container>
</template>

<script>
  import {audit_tasklist, userinfo, audit_taskconfirm, auditTask_refuse} from '@/api/api'
  import moment from "moment";
  export default {
    name: "AdminRefundList",
    data() {
      return {
        tableData: [],
        searchList: {
          searchValue: "",
          timelist: [],
          pn: 1,
          Count: 0,
          pagesize:100
        },
        multiple:[],
        radio1: '1',
        page: 1,
        loading: false,
        statusVal: 1,
        pagesize:100,
        statuslist: [
          {
            value: 0,
            label: '全部'
          },
          {
            value: 1,
            label: '等待审核'
          }, {
            value: 2,
            label: '审核通过'
          }, {
            value: 3,
            label: '拒绝审核'
          }],
        statustype: [
          {
            value: 1,
            label: '淘宝'
          }, {
            value: 2,
            label: '拼多多'
          }, {
            value: 3,
            label: '抖音'
          }],
      }
    },
    mounted() {
      let params = {
        page: this.page,
        page_size: this.pagesize,
        status: this.statusVal,
        type: Number(this.radio1)
      }
      this.getorderList(params)
    },
    methods: {
      handleSizeChange(val){
        let params = {
          page:this.page,
          page_size: val,
          status: this.statusVal,
          type: Number(this.radio1)
        }
        this.getorderList(params)
      },
      exportToExcel() {
        require.ensure([], () => {
          const {
            export_json_to_excel
          } = require('../../../excel/Export2Excel');
          const tHeader = ['序号', '订单编号','订单类型', '店铺名字', '刷手会员名','刷手手机号', '宝贝ID', '商品金额', '创建时间'];
          const filterVal = ['id', 'task_order','task_class','task_shopname','brushhands_tbname','phone',  'baby_id', 'task_bjmoney', 'create_time'];
          var arring = this.multiple
          const list = arring;
          const data = this.formatJson(filterVal, list);
          export_json_to_excel(tHeader, data, '订单审核excel');
        })
      },
      formatJson(filterVal, jsonData) {
        return jsonData.map(v => filterVal.map(j => v[j]))
      },
      //获取用户信息
      getUser() {
        userinfo().then(res => {
          if (res.Status == 200) {
            this.$emit('getMoney', res.Data.money)
          } else {
            this.$message.error(res.Message)
          }
        })
      },
      handleSelectionChange(val){
          this.multiple = val
      },
      //分页
      pnChange(pn) {
        this.page = pn
        let params = {
          order: this.searchList.searchValue,
          time: this.searchList.timelist,
          page: pn,
          page_size: this.pagesize,
          status: this.statusVal,
          type: Number(this.radio1)
        }
        this.getorderList(params)
      },
      //获取订单列表
      getorderList(params) {
        audit_tasklist(params).then(res => {
          this.searchList = res
          if (res.Status == 200) {
            this.tableData = res.Data
            let ipos
            this.tableData.forEach(item => {
              ipos = item.create_time.indexOf("+")
              item.create_time = item.create_time.replace(/T/g, " ").substring(0, ipos)
            })
          } else {
            this.$message.error(res.Message)
          }
        })
      },
      //查询
      init(val) {
        console.log(this.searchList);
        let params = {
          order: this.searchList.searchValue,
          time: this.searchList.timelist,
          page: this.page,
          page_size: this.pagesize,
          status: this.statusVal,
          type: val
        }
        this.getorderList(params)
      },
      initing() {
        let params = {
          order: this.searchList.searchValue,
          time: this.searchList.timelist,
          page: this.page,
          page_size:100,
          status: this.statusVal,
          type: Number(this.radio1)
        }
        this.getorderList(params)
      },
      //通过
      onPass(row, idx) {
        let params = {
          order: row.task_order,
          task_id: row.id,
          address: sessionStorage.getItem('ip'),
          type: Number(this.radio1)
        }
        if (idx === 1) {
          audit_taskconfirm(params).then(res => {
            if (res.Status == 200) {
              this.$message({
                type: 'success',
                message: res.Message
              });
              let param = {
                page: this.page,
                page_size: this.pagesize,
                status: this.statusVal,
                type: Number(this.radio1)
              }
              this.getorderList(param)
            } else {
              this.$message({
                type: 'error',
                message: res.Message
              });
            }
          })
        } else {
          this.open(params)
        }
      },
      //拒绝
      open(params) {
        this.$prompt('请输入拒绝原因', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
        }).then(({value}) => {
          params.note = value
          auditTask_refuse(params).then(res => {
            if (res.Status == 200) {
              this.$message({
                type: 'success',
                message: res.Message
              });
              this.init()
            } else {
              this.$message({
                type: 'error',
                message: res.Message
              });
            }
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '取消输入'
          });
        });
      }
    }
  }
</script>

<style scoped>
</style>
